package cn.iocoder.devlop.module.annual.report.sfcbranchinfo.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.devlop.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.devlop.module.annual.report.baseinfo.dal.dataobject.BaseinfoDO;
import cn.iocoder.devlop.module.annual.report.baseinfo.service.BaseinfoService;
import cn.iocoder.devlop.module.annual.report.sfcbranchinfo.controller.vo.SfcBranchinfoCreateReqVO;
import cn.iocoder.devlop.module.annual.report.sfcbranchinfo.controller.vo.SfcBranchinfoOverwriteVO;
import cn.iocoder.devlop.module.annual.report.sfcbranchinfo.convert.SfcBranchinfoConvert;
import cn.iocoder.devlop.module.annual.report.sfcbranchinfo.dal.SfcBranchinfoMapper;
import cn.iocoder.devlop.module.annual.report.sfcbranchinfo.dal.dataobject.SfcBranchinfoDO;
import cn.iocoder.devlop.module.annual.report.utils.IdGenerateUtil;
import cn.iocoder.devlop.module.annual.report.utils.ObjectComparisonUtil;
import cn.iocoder.devlop.module.annual.report.utils.ReportYearHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 年报农合分支机构信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class SfcBranchinfoServiceImpl implements SfcBranchinfoService {

    @Resource
    private SfcBranchinfoMapper sfcBranchinfoMapper;

    @Resource
    private BaseinfoService baseinfoService;

    @Override
    public List<SfcBranchinfoDO> getSfcBranchinfoList(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        return sfcBranchinfoMapper.selectListByAncheid(ancheid);
    }

    @Override
    public void deleteSfcBranchinfoAll(String regno, String ancheyear) {
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        LambdaQueryWrapperX<SfcBranchinfoDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(SfcBranchinfoDO::getAncheid,ancheid);
        sfcBranchinfoMapper.delete(wrapperX);
    }


    @Override
    public void createSfcBranchinfoByList(String regno, String ancheyear, List<SfcBranchinfoCreateReqVO> sfcBranchinfoList) {
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        if (CollUtil.isNotEmpty(sfcBranchinfoList)) {
            List<SfcBranchinfoDO> sfcBranchinfoDOS = SfcBranchinfoConvert.INSTANCE.convertList01(sfcBranchinfoList);
            for (SfcBranchinfoDO sfcBranchinfo : sfcBranchinfoDOS) {
                sfcBranchinfo.setAncheid(ancheid);
                sfcBranchinfo.setBrid(IdUtil.simpleUUID());
            }
            sfcBranchinfoMapper.insertBatch(sfcBranchinfoDOS);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overwriteSfcBranchinfo(SfcBranchinfoOverwriteVO updateReqVO) {
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        if (ObjUtil.isNotEmpty(baseinfo)){
            if (StrUtil.equals(baseinfo.getState(),"02") ||StrUtil.equals(baseinfo.getState(),"03")) {
                List<SfcBranchinfoDO> oldSfcBranchinfoList = getSfcBranchinfoList(updateReqVO.getRegno(), updateReqVO.getAncheyear());
                List<SfcBranchinfoCreateReqVO> newAlterstockinfoList = updateReqVO.getSfcBranchinfoList();
                List<SfcBranchinfoCreateReqVO> oldreqs = SfcBranchinfoConvert.INSTANCE.convertList3(oldSfcBranchinfoList);
                ObjectComparisonUtil.compareObjectList(oldreqs,newAlterstockinfoList,IdGenerateUtil.getAncheid(updateReqVO.getRegno(), updateReqVO.getAncheyear()),"股权变更");
            }
        }
        deleteSfcBranchinfoAll(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        createSfcBranchinfoByList(updateReqVO.getRegno(), updateReqVO.getAncheyear(), updateReqVO.getSfcBranchinfoList());
        baseinfoService.updateState(updateReqVO.getRegno(), updateReqVO.getAncheyear());
    }

}
